import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dingding/pages/address_book/index.dart';
import 'package:flutter_treeview/flutter_treeview.dart';
import 'package:get/get.dart' as getx;
import 'package:get/get_navigation/get_navigation.dart';

import '../../core/widget/custom_app_bar.dart';

class AddressBookTree extends StatefulWidget {
  AddressBookTree({Key? key}) : super(key: key);

  @override
  _AddressBookTreeState createState() => _AddressBookTreeState();
}

class _AddressBookTreeState extends State<AddressBookTree> {
  String _selectedNode = '';
  List<Node> _nodes = [];
  late TreeViewController _treeViewController;

  bool docsOpen = true;
  bool deepExpanded = true;

  ExpanderPosition _expanderPosition = ExpanderPosition.start;
  ExpanderType _expanderType = ExpanderType.caret;
  ExpanderModifier _expanderModifier = ExpanderModifier.none;
  bool _allowParentSelect = false;
  bool _supportParentDoubleTap = false;

  @override
  void initState() {
    _nodes = [
      Node(
        key: 'company',
        label: '公司',
        icon: Icons.home,
        expanded: true,
        parent: true,
        children: [
          Node(
            key: 'developmentDepartment',
            label: '技术部',
            icon: Icons.home_outlined,
            parent: false,
          ),
          Node(
            key: 'financeDepartment',
            label: '财务部',
            icon: Icons.home_outlined,
            parent: false,
          ),
          Node(
            key: 'administrationDepartment',
            label: '行政部',
            icon: Icons.home_outlined,
            parent: false,
          ),
        ]
      )
    ];
    _treeViewController = TreeViewController(
      children: _nodes,
      selectedKey: _selectedNode,
    );

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    TreeViewTheme _treeViewTheme = TreeViewTheme(
      expanderTheme: ExpanderThemeData(
          type: _expanderType,
          modifier: _expanderModifier,
          position: _expanderPosition,
          // color: Colors.grey.shade800,
          size: 20,
          color: Color(0xFF333333)
      ),
      labelStyle: TextStyle(
        fontSize: 16,
        letterSpacing: 0.3,
      ),
      parentLabelStyle: TextStyle(
        fontSize: 16,
        letterSpacing: 0.1,
        fontWeight: FontWeight.w800,
        color: Color(0xFF333333)
      ),
      iconTheme: IconThemeData(
        size: 18,
        color: Color(0xFF333333)
      ),

      // colorScheme: Theme.of(context).colorScheme,
      colorScheme: ColorScheme.light(primary: Colors.transparent, onPrimary: Colors.black)

    );
    return Scaffold(
      appBar: customAppbar(context:context, title: '通讯录', borderBottom: false),
      body: TreeView(
        controller: _treeViewController,
        allowParentSelect: _allowParentSelect,
        supportParentDoubleTap: _supportParentDoubleTap,
        onExpansionChanged: (key, expanded) {
          _expandNode(key, expanded);
        },
        onNodeTap: (key) {
          debugPrint('Selected: $key');
          setState(() {
            _selectedNode = key;
            _treeViewController =
                _treeViewController.copyWith(selectedKey: key);
          });
          getx.Get.to(() => AddressBook(), transition: Transition.rightToLeft);
        },
        theme: _treeViewTheme,
      ),
    );
  }

  _expandNode(String key, bool expanded) {

    String msg = '${expanded ? "Expanded" : "Collapsed"}: $key';
    debugPrint(msg);
    Node? node = _treeViewController.getNode(key);
    if (node != null) {
      List<Node> updated;
      updated = _treeViewController.updateNode(
          key, node.copyWith(expanded: expanded));
      // if (key == 'docs') {
      //   updated = _treeViewController.updateNode(
      //       key,
      //       node.copyWith(
      //         expanded: expanded,
      //         icon: expanded ? Icons.folder_open : Icons.folder,
      //       ));
      // } else {
      //   updated = _treeViewController.updateNode(
      //       key, node.copyWith(expanded: expanded));
      // }
      setState(() {
        // if (key == 'docs') docsOpen = expanded;
        _treeViewController = _treeViewController.copyWith(children: updated);
      });
    }

  }
}

